package com.hzdl.house_mgr.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.house_mgr.bean.Admin;
import com.hzdl.house_mgr.bean.Role;
import com.hzdl.house_mgr.service.AdminService;
import com.hzdl.house_mgr.service.RoleService;
import com.hzdl.house_mgr.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("sysAdmin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("admin/adminRightJson")
    public AjaxResult adminRightJson(Admin admin, Integer page, Integer limit){
        PageInfo<Admin> pageInfo = adminService.findAdminByWhere(admin,page, limit);
        Integer count = adminService.getCountByWhere(admin);
        return AjaxResult.ok(count,pageInfo.getList());
    }

    @RequestMapping("admin/adminAddOrUpdate")
    public AjaxResult adminAddOrUpdate(Admin admin){

        if(admin.getAdminId() == null){
            int i = adminService.insertAdmin(admin);
            if(i>0){
                return AjaxResult.ADD_SUCCESS();
            }else{
                return AjaxResult.ADD_FAILED();
            }
        }else{
            int i = adminService.updateAdmin(admin);
            if(i>0){
                return AjaxResult.UPD_SUCCESS();
            }else{
                return AjaxResult.UPD_FAILED();
            }
        }
    }

    @RequestMapping("admin/adminDelete")
    public AjaxResult adminDelete(Integer id){
        int i = adminService.deleteAdminById(id);
        if(i>0){
            return AjaxResult.DEL_SUCCESS();
        }else{
            return AjaxResult.DEL_FAILED();
        }
    }

    @RequestMapping("admin/resetAdminPwd")
    public AjaxResult resetAdminPwd(Integer id){
        int i = adminService.resetAdminPwd(id);
        if(i>0){
            return AjaxResult.RESETPWD_SUCCESS();
        }else{
            return AjaxResult.RESETPWD_FAILED();
        }
    }

    @RequestMapping("admin/deleteSomeAdmin")
    public AjaxResult deleteSomeAdmin(String adminIds){
        int i = adminService.deleteSomeAdmin(adminIds.split(","));
        if(i>0){
            return AjaxResult.DELSOME_SUCCESS();
        }else{
            return AjaxResult.DELSOME_FAILED();
        }
    }

    @RequestMapping("admin/initAdminRole")
    public AjaxResult initAdminRole(String roleName,String roleDesc,Integer page,Integer limit,String adminId){
        PageInfo<Role> pageInfo = roleService.findRoleByWhere(roleName,roleDesc, page, limit);
        Integer count = roleService.getCountByWhere(roleName,roleDesc);
        return AjaxResult.ok(count,pageInfo.getList());
    }

    @RequestMapping("admin/roleAllJson")
    public AjaxResult roleAllJson(Integer adminId){
        //获取所有角色
        List<Role> all = roleService.findAll();

        //获取当前用户的角色
        List<Role> RoleByAdminId = roleService.findRoleByAdminId(adminId);

        List<Map<String, Object>> list = new ArrayList<>();
        for(Role r:all){
            Map<String, Object> map = new HashMap<>();
            // 把当前r 在 roleByAdminId集合中去查找，如果存在就把属性LAY_CHECKED设为true
            Boolean flag = false;
            for (Role r2 : RoleByAdminId){
                if(r.getRoleId() == r2.getRoleId()){
                    flag = true;
                    break;
                }
            }
            map.put("roleId",r.getRoleId());
            map.put("roleName",r.getRoleName());
            map.put("roleDesc",r.getRoleDesc());
            map.put("available",r.getAvailable());
            map.put("LAY_CHECKED",flag);

            list.add(map);
        }

        return AjaxResult.ok(list);
    }

    @RequestMapping("admin/updRoleByAdminId")
    public String updRoleByAdminId(Integer adminId,String roleIds){
        String[] split = roleIds.split(",");
        Integer[] arr = new Integer[split.length];
        for(int i = 0;i<arr.length;i++){
            arr[i] = Integer.valueOf(split[i]);
        }
        int i = adminService.updateRoleAdmin(adminId, arr);
        if(i>0){
            return "1";
        }else{
            return "";
        }
    }
}
